/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.herenit.beijing.dh2011.util;

import com.herenit.beijing.poc.serverfactory.ServerFactory;
import com.sun.ejb.EJBUtils;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.openide.util.NbBundle;
import org.openide.util.lookup.ServiceProvider;

/**
 *
 * @author Obama
 */
@ServiceProvider(service = ServerFactory.class)
public class EJBServiceFactory implements ServerFactory {

    public Properties properties;

    public Properties getContext() {
        properties = new Properties();
        properties.setProperty("org.omg.CORBA.ORBInitialHost", NbBundle.getMessage(EJBServiceFactory.class, "org.omg.CORBA.ORBInitialHost"));
        properties.setProperty("org.omg.CORBA.ORBInitialPort", NbBundle.getMessage(EJBServiceFactory.class, "org.omg.CORBA.ORBInitialPort"));
        properties.setProperty("jnp.timeout", NbBundle.getMessage(EJBServiceFactory.class, "jnp.timeout"));
        properties.setProperty("jnp.sotimeout", NbBundle.getMessage(EJBServiceFactory.class, "jnp.sotimeout"));
        properties.setProperty("com.sun.corba.ee.transport.ORBWaitForResponseTimeout", NbBundle.getMessage(EJBServiceFactory.class, "com.sun.corba.ee.transport.ORBWaitForResponseTimeout"));
        return properties;
    }

    public EJBServiceFactory() {
        super();
        getContext();
    }

    @Override
    public Object getServer(String clazz, String name) {
        Object obj = null;
        ClassLoader prev = Thread.currentThread().getContextClassLoader();
        try {
            Thread.currentThread().setContextClassLoader(EJBUtils.class.getClassLoader());
            Context context = new InitialContext(properties);
            
            obj = context.lookup(name);
        } catch (NamingException ex) {
            ex.printStackTrace();
        } finally {
            Thread.currentThread().setContextClassLoader(prev);
            return obj;
        }
    }
}
